//
//  LYKRootViewController.m
//  MusicTai
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 liyinkai. All rights reserved.
//

#import "LYKRootViewController.h"

@interface LYKRootViewController ()<UIScrollViewDelegate>

@end

static bool isOut;

@implementation LYKRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    isOut = NO;
    
    [self makeLauchView];
}

- (void) makeLauchView {
    NSArray *imgArray = [NSArray arrayWithObjects:@"page1.jpg", @"page2.jpg", @"page3.jpg", @"page4.jpg",nil];
    UIScrollView *scr = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scr.contentSize = CGSizeMake(self.view.bounds.size.width * 4, self.view.bounds.size.height);
    scr.bounces = NO;
    scr.showsHorizontalScrollIndicator = NO;
    scr.showsVerticalScrollIndicator = NO;
    scr.tag = 10002;
    scr.pagingEnabled = YES;
    scr.delegate = self;
    [self.view addSubview:scr];
    
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 20)];
    page.tag = 10001;
    [page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    page.backgroundColor = [UIColor clearColor];
    page.numberOfPages = imgArray.count;
    page.currentPage = 0;
    
    [self.view addSubview:page];
    for (int i = 0; i < imgArray.count; i++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        img.userInteractionEnabled = YES;
        img.image = [UIImage imageNamed:imgArray[i]];
        [scr addSubview:img];
        
        if (i == imgArray.count - 1) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = self.view.bounds;
//            [button setTitle:@"立即体验" forState:UIControlStateNormal];
            button.layer.borderWidth = 1.0;
            button.layer.borderColor = [UIColor whiteColor].CGColor;
//            button.layer.cornerRadius = button.bounds.size.height / 2;
            [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
            [img addSubview:button];
            [button bringSubviewToFront:img];
        }
    }
    
}

- (void)pageChange:(UIPageControl *)page{
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:10002];
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * page.currentPage, 0) animated:YES];
}

- (void) buttonDidPress:(UIButton *)button {
    [self.mydelegate pushOtherView];
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (isOut) {
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha = 0;
        } completion:^(BOOL finished) {
            [scrollView removeFromSuperview];
        }];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    if (scrollView.contentOffset.x > 3 * self.view.width + 20) {
    //        isOut = YES;
    //    }
    int index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    UIPageControl *page = [(UIPageControl *)self.view viewWithTag:10001];
    page.currentPage = index;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
